package com.syh.jn.scm.service.impl.supplier.finance;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.finance.SalesInvoice;
import com.syh.jn.scm.domain.supplier.event.finance.SalesInvoiceEvent;
import com.syh.jn.scm.domain.supplier.repository.finance.SalesInvoiceRepository;
import com.syh.jn.scm.dto.supplier.finance.SalesInvoiceMto;
import com.syh.jn.scm.dto.supplier.finance.SalesInvoiceSto;
import com.syh.jn.scm.dto.supplier.finance.SalesInvoiceVo;
import com.syh.jn.scm.service.api.supplier.finance.SalesInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销项发票接口实现
 */
@Service
@Transactional
public class SalesInvoiceServiceImpl extends AbstractServiceImpl<SalesInvoiceSto, SalesInvoiceMto, SalesInvoiceVo, SalesInvoice, Long> implements SalesInvoiceService {

  @Autowired
  private SalesInvoiceRepository salesInvoiceRepositoryImpl;

  @Override
  protected AggregateRepository<SalesInvoice, Long> getAggregateRepository() {
    return salesInvoiceRepositoryImpl;
  }

  /**
   * 新增发票信息
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(SalesInvoiceSto sto) {
    SalesInvoice invoice = new SalesInvoice(sto);
    Long id = salesInvoiceRepositoryImpl.add(invoice);
    this.publicEvent(SalesInvoiceEvent.buildCreated(new Object[]{invoice}));
    return id;
  }

  /**
   * 修改发票信息
   *
   * @param mto
   */
  @Override
  public void update(SalesInvoiceMto mto) {
    SalesInvoice invoice = salesInvoiceRepositoryImpl.getById(mto.getId());
//    SalesInvoice oldInvoice = new SalesInvoice();
//    ExtBeanUtils.copyProperties(invoice, oldInvoice);
    invoice.modify(mto);
    salesInvoiceRepositoryImpl.update(invoice);
//    this.publicEvent(SalesInvoiceEvent.buildUpdate(new Object[]{oldInvoice, invoice}));
  }

  /**
   * 删除发票信息
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    SalesInvoice invoice = salesInvoiceRepositoryImpl.getById(id);
    invoice.destroy();
    salesInvoiceRepositoryImpl.remove(invoice);
    this.publicEvent(SalesInvoiceEvent.buildDestroy(new Object[]{invoice}));
  }

  /**
   * 签收
   *
   * @param id
   */
  @Override
  public void confirm(Long id) {
    SalesInvoice invoice = salesInvoiceRepositoryImpl.getById(id);
    invoice.confirm();
    salesInvoiceRepositoryImpl.update(invoice);
    //更改对账单的 已开票金额
    //发送事件消息
    this.publicEvent(SalesInvoiceEvent.salesInvoiceConfirm(new Object[]{invoice}));
  }

  /**
   * 取消签收
   *
   * @param id
   */
  @Override
  public void cancel(Long id) {
    SalesInvoice invoice = salesInvoiceRepositoryImpl.getById(id);
    invoice.cancel();
    salesInvoiceRepositoryImpl.update(invoice);
    this.publicEvent(SalesInvoiceEvent.salesInvoiceUnconfirm(new Object[]{invoice}));
  }

  /**
   * 解锁
   *
   * @param id
   */
  @Override
  public void unLock(Long id) {
    SalesInvoice invoice = salesInvoiceRepositoryImpl.getById(id);
    invoice.unLock();
    salesInvoiceRepositoryImpl.update(invoice);
  }
}
